package com.atung.parking.mapstruct;

import com.atung.parking.entity.BusApprovalRecord;
import com.atung.parking.entity.BusParkingIot;
import com.atung.parking.enums.ParkingIotApprovalState;
import com.atung.parking.export.ParkingIotExportVO;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.ParkingIotResponse;
import com.atung.parking.response.page.SimpleParkingIotResponse;
import org.mapstruct.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 */
@Component
@Mapper(componentModel = "spring")
@DecoratedWith(ParkingIotConverterDecorator.class)
public interface ParkingIotConverter {
    ParkingIotResponse toResponse(BusParkingIot busParkingIot);

    SimpleParkingIotResponse toSimpleResponse(BusParkingIot busParkingIot);

    ParkingIotExportVO toExportVo(BusParkingIot busParkingIot);

    ApprovalRecordResponse toApprovalResponse(BusApprovalRecord busApprovalRecord);

    @Named("timestampToString")
    default String timestampToString(String timestamp) {
        if (timestamp == null || timestamp.isEmpty()) {
            return null;
        }
        try {
            long timeInMillis = Long.parseLong(timestamp);
            Date date = new Date(timeInMillis);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } catch (NumberFormatException e) {
            // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
            return null;
        }
    }

    @Named("setApprovalStatus")
    default String setApprovalStatus(Integer approvalStatus) {
        String descriptionByCode = ParkingIotApprovalState.getDescriptionByCode(approvalStatus);
        return descriptionByCode;
    }
}
